package com.xinmao.quantitative.trad.strategies.combine;

import com.xinmao.quantitative.trad.strategies.IStrategy;
import org.springframework.stereotype.Component;
import org.ta4j.core.BarSeries;
import org.ta4j.core.BaseStrategy;
import org.ta4j.core.Rule;
import org.ta4j.core.Strategy;
import org.ta4j.core.indicators.EMAIndicator;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.indicators.volume.ChaikinMoneyFlowIndicator;
import org.ta4j.core.num.DecimalNum;
import org.ta4j.core.num.Num;
import org.ta4j.core.rules.*;


/**
 * 5-8-13-ema-crossover Strategy
 *
 * @see <a href="https://school.stockcharts.com/doku.php?id=technical_indicators:moving_averages:5-8-13-ema-crossover">
 *      https://school.stockcharts.com/doku.php?id=technical_indicators:moving_averages:5-8-13-ema-crossover</a>
 */
@Component
public class EMA5813Strategy implements IStrategy {


    @Override
    public  Strategy buildStrategy(BarSeries series) {
        ClosePriceIndicator closePrice = new ClosePriceIndicator(series);

        EMAIndicator ema5 = new EMAIndicator(closePrice, 5);
        EMAIndicator ema8 = new EMAIndicator(closePrice, 8);
        EMAIndicator ema13 = new EMAIndicator(closePrice, 13);

        ChaikinMoneyFlowIndicator cmf = new ChaikinMoneyFlowIndicator(series, 20);


        //看涨信号。当 5 EMA 越过 8 EMA 并且两者都高于 13 EMA 时。
        Rule entryRule = new OverIndicatorRule(ema5, ema8) // Bull trend
                .and(new CrossedUpIndicatorRule(ema8, ema13))
                .and(new UnderIndicatorRule(cmf, -0.05));


        //看跌信号。相反，当 5 EMA 穿过 8 EMA 时。
        Rule exitRule = new CrossedDownIndicatorRule(ema5, ema8)
                .and(new OverIndicatorRule(cmf, 0.05));

        return new BaseStrategy(entryRule, exitRule);
    }

    @Override
    public Num getScore() {
        //1.3
        return DecimalNum.valueOf(1);
    }
}
